﻿
$(document).ready(function () {


    // $("input#txtAmountModal").numeric();
    $("#input-modal span#vldAmount").hide();

    $("#input-modal input#btnOK").click(function () {
        if (!validateAmount_60pt($("#input-modal input#txtAmountModal"))) {
            $("#input-modal span#vldAmount").show();
            return;
        }
        else {
            $("#input-modal span#vldAmount").hide();
        }


        processInput();
        $.fancybox.close();
    });

    $("#input-modal input#btnCancel").click(function () {
        $.fancybox.close();
    });

    $("#input-modal select#ddlFlowSystem").on('change', function () {
        var val = $(this).val();

        var item_type = $.trim(val.split('|')[0]);
        var item_id = $.trim(val.split('|')[1]);

        var select = $("select#ddlUnitModal", $(this).closest("#input-modal"));
        filterUnitDropdown(item_type, item_id, select);
    });
});

function resetInputModal() {
    $("#input-modal select#ddlFlowSystem option:eq(0)").prop('selected', 'selected');
    $("#input-modal input#txtAmountModal").val('');
    $("#input-modal select#ddlFlowSystem").trigger("change");
    // $("#input-modal select#ddlUnitModal option:eq(0)").prop('selected', 'selected');
}

function loadInputModal(selectedRow) {
    $("#input-modal select#ddlFlowSystem").val(selectedRow.item_type + "|" + selectedRow.item_id);
    $("#input-modal input#txtAmountModal").val(selectedRow.amount);
    $("#input-modal select#ddlFlowSystem").trigger("change");
    $("#input-modal select#ddlUnitModal").val(selectedRow.unit_id);
}

function processInput() {
    var str = $("#input-modal select#ddlFlowSystem").val();
    var item_id = $.trim(str.split('|')[1]);
    var item_type = $.trim(str.split('|')[0]);
    var item_name = $.trim($("#input-modal select#ddlFlowSystem option:selected").text().split('-')[1]);


    var amount = $("#input-modal input#txtAmountModal").val();
    var unit_id = $("#input-modal select#ddlUnitModal").val();
    var unit_name = $("#input-modal select#ddlUnitModal option:selected").text();
    unit_name = $.trim(unit_name.split('-')[1]);

    var row = {
        item_id: item_id,
        item_type: item_type,
        item_name: item_name,
        amount: amount,
        unit_id: unit_id,
        unit_name: unit_name
    };

    callbackFromInputModal(row);
    // console.log("processInput: " + str + " | " + amount + " | " + unit_id + " | " + unit_name);
}
                    
